/*
 * Copyright (C) 2000-2008 Jens Nixdorf and various contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.mopore.jat.timer.creation;

import static org.mopore.jat.timer.TimingType.COUNT_UP;
import static org.mopore.jat.timer.TimingType.CUSTOM_COUNT_DOWN;
import static org.mopore.jat.timer.TimingType.FOUR_DIGITS_COUNT_DOWN;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.mopore.jat.gui.SelectTextFocusAdapter;
import org.mopore.jat.gui.wizard.AbstractWizardPage;
import org.mopore.jat.gui.wizard.ButtonPanel;
import org.mopore.jat.gui.wizard.WizardContext;
import org.mopore.jat.timer.TimingType;

public class FinishPage extends AbstractWizardPage<TimingCreateProgress> {
	
	private static final Map<TimingType, 
	Class<? extends AbstractWizardPage<TimingCreateProgress>>> navMap = 
		new HashMap<TimingType, 
		Class<? extends AbstractWizardPage<TimingCreateProgress>>>(){
			{
				put( FOUR_DIGITS_COUNT_DOWN, FourDigitsPage.class );
				put( CUSTOM_COUNT_DOWN, CustomCountDownPage.class );
				put( COUNT_UP, StartPage.class );
			}
		};
	private JCheckBox showInTrayCheckBox;
	private JCheckBox showMessageCheckBox;
	private JTextField messageTextField;
	
	
	public FinishPage() {
		
		ButtonPanel buttonPanel = getButtonPanel();
		buttonPanel.enableFinishButton();
		buttonPanel.enableBackButton();
		buttonPanel.addActionToBackButton( new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				
				WizardContext<TimingCreateProgress> context = context();

				TimingCreateProgress progress = context.get();
				TimingType type = progress.getNewCountDownType();
				Class<? extends AbstractWizardPage<TimingCreateProgress>> targetPage =
					navMap.get( type );
				changePage( targetPage );
			}
		} );
		
	}


	@Override
	public JPanel getContentPanel(){
		
		final TimingCreateProgress progress = context().get();
		TimingType type = progress.getNewCountDownType();
		
		JPanel panel = new JPanel();
		
		GridLayout gridLayout = new GridLayout( 3, 1 );
		panel.setLayout( gridLayout );
		
		boolean showInTray = progress.isShowInTray();
		
		showInTrayCheckBox = new JCheckBox( "show in tray when active" );
		showInTrayCheckBox.setSelected( showInTray );
		panel.add( showInTrayCheckBox );
		
		showMessageCheckBox = new JCheckBox( "show alert message" );
		showMessageCheckBox.setSelected( true );
		panel.add( showMessageCheckBox );
		
		
		String messageText = progress.getMessageText();
		messageTextField = new JTextField( messageText );
		FocusListener focusListener = 
			new SelectTextFocusAdapter( messageTextField );
		messageTextField.addFocusListener( focusListener );
		panel.add( messageTextField );
		
		showMessageCheckBox.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boolean showMessageText = showMessageCheckBox.isSelected();
				messageTextField.setEnabled( showMessageText );
			}
		} );
		
		if ( COUNT_UP == type ){
			showMessageCheckBox.setEnabled( false );
			messageTextField.setEnabled( false );
		}
		
		panel.setBorder( BorderFactory.createEmptyBorder( 0, 10, 0, 10) );
		
		return panel;
	}
	
	
	@Override
	public void storeToContext() {
		
		WizardContext<TimingCreateProgress> context = context();
		TimingCreateProgress progress = context.get();
		
		boolean  showInTray = showInTrayCheckBox.isSelected();
		progress.setShowInTray( showInTray );
		
		boolean  showMessageText = showMessageCheckBox.isSelected();
		progress.setShowMessageText( showMessageText );
		
		String messageText = messageTextField.getText();
		progress.setMessageText( messageText );
	}

}
